﻿using System;
using System.IO;
using System.Windows.Forms;

namespace DominionWar
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void fleet1_btn_Click(object sender, EventArgs e)
        {
            if (openFleetFileDialog.ShowDialog() == DialogResult.OK)
            {
                fleet1_txtbox.Text = openFleetFileDialog.FileName;
            }
        }

        private void fleet2_btn_Click(object sender, EventArgs e)
        {
            if (openFleetFileDialog.ShowDialog() == DialogResult.OK)
            {
                fleet2_txtbox.Text = openFleetFileDialog.FileName;
            }
        }

        private void runsimulation_btn_Click(object sender, EventArgs e)
        {
            report_txtbox.Text = ""; // Clear report textbox

            // Checks textbox inputs for fleet file names are valid, and then seed value, in order
            if (fleet1_txtbox.Text == "")
                DisplayErrorBox("No file entered for fleet 1");
            else if (File.Exists(fleet1_txtbox.Text) == false)
                DisplayErrorBox(fleet1_txtbox.Text + " does not exist");
            else if (fleet2_txtbox.Text == "")
                DisplayErrorBox("No file entered for fleet 2");
            else if (File.Exists(fleet2_txtbox.Text) == false)
                DisplayErrorBox(fleet2_txtbox.Text + " does not exist");
            else if (seedvalue_txtbox.Text == "")
                DisplayErrorBox("No seed value entered");
            else // All textboxes have values entered
            {
                try
                {
                    Simulation simulation = new Simulation(seedvalue_txtbox.Text, fleet1_txtbox.Text, fleet2_txtbox.Text);
                    report_txtbox.Text = simulation.getBattleReport();
                }
                catch (SimulationError ex)
                {
                    DisplayErrorBox(ex.ToString());
                }
            }
        }

        // Displays the error box popups to the user
        private void DisplayErrorBox(String msg)
        {
            String messageText = "The program failed with the following error\r\n" + msg;
            String messageTitle = "Error";
            MessageBox.Show(messageText, messageTitle, MessageBoxButtons.OK, MessageBoxIcon.Error);
        }

        private void report_txtbox_TextChanged(object sender, EventArgs e)
        {
            // Automatically jumps the cursor position to the end and scrolls the textbox to it
            // whenever a report update occurs
            report_txtbox.Select(report_txtbox.Text.Length, 0);
            report_txtbox.ScrollToCaret();
        }
    }
}
